package com.candy.niuniu.logdubbo.service.impl;

import com.candy.niuniu.logapidubbo.LogService;
import com.candy.niuniu.logdubbo.entity.LogLink;
import com.candy.niuniu.logdubbo.service.LogLinkService;
import lombok.AllArgsConstructor;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author suYanLong
 * @version 1.0
 * @date 2023/10/9 16:30
 * @Description 描述
 */
@RestController
@RequestMapping("log")
//@DubboService(token = "false", protocol = {"rest"})
@DubboService(token = "false", protocol = {"dubbo"})
//@DubboService(token = "false", protocol = {"dubbo,rest"})
@AllArgsConstructor
public class LogServiceImpl implements LogService {

    private static Logger logger = LoggerFactory.getLogger(LogServiceImpl.class);

    private final LogLinkService logLinkService;

    @Override
    @GetMapping("saveLog")
//    public void saveLog(String logStr) {
    public String saveLog(@RequestParam("logStr") String logStr) {
        String server = RpcContext.getServerAttachment().getAttachment("server");
        logger.info("server : " + server);
        logger.info("provider save: " + logStr);
        RpcContext.getServerContext().setAttachment("result", "logDubbo");
        LogLink logLink = new LogLink().setId(System.currentTimeMillis()).setServiceName(logStr);
        logLinkService.save(logLink);
        return "logDubbo save success :" + logStr;
    }

}
